<template>
  <div>
    <el-button type="primary" @click="getLists()">添加</el-button>
    <el-dialog v-el-drag-dialog :visible.sync="dialogTableVisible" title="添加" width="900px">
      <el-form ref="row_data" :model="row_data" :rules="rules" label-position="left" label-width="100px" style="width: 750px; margin-left:50px;">
        <el-form-item :label="'语录记录'" prop="ana_record">
          <el-input v-model="row_data.ana_record" placeholder="语录记录"/>
        </el-form-item>
        <el-form-item :label="'语录分类'" prop="classify_id">
          <el-select v-model="row_data.classify_id" filterable placeholder="输入关键词">
            <el-option
              v-for="classify in classify_list"
              :key="classify.id"
              :label="classify.classify"
              :value="classify.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm()">立即创建</el-button>
          <el-button @click="close()">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import elDragDialog from '@/directive/el-dragDialog' // base on element-ui
import { add, get_classify_list } from '@/api/poster/ana'
import store from '@/store'

export default {
  name: 'DragDialogDemo',
  directives: { elDragDialog },
  data() {
    return {
      rules: {
        classify_id: [
          { required: true, message: '语录分类不能为空', trigger: 'blur' }
        ]
      },
      dialogTableVisible: false,
      row_data: {
        status: 1
      },
      classify_list: []
    }
  },
  watch: {
    classifyList: {
      handler() {
        this.classify_list = this.classifyList
      },
      deep: true
    }
  },
  beforeMount() {
    this.classify_list = this.classifyList
  },
  methods: {
    getLists() {
      this.dialogTableVisible = true
      // 获取语录分类数据
      get_classify_list(store.getters.token).then(res => {
        console.log('get_classify_list', res.data.list)
        this.classify_list = res.data.list
      })
      // console.log('123333333')
    },
    submitForm() {
      this.$refs.row_data.validate(valid => {
        if (valid) {
          const data = this.row_data
          add(store.getters.token, data).then(res => {
            if (res.data.status) {
              this.$message.success('添加成功！')
              this.$emit('getLists')
              this.close()
            } else {
              this.$message.error('添加失败！原因：' + res.row_data.msg)
            }
          })
        } else {
          return false
        }
      })
    },
    close() {
      this.dialogTableVisible = false
    }
  }
}
</script>

